package wangjg.android;

import java.util.Date;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class ChatActivity extends Activity {

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        this.setContentView(R.layout.chat_list);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.chat_titlebar);
        this.init();
    }
    
    private void init(){
		LinearLayout ll = (LinearLayout) this.findViewById(R.id.chat_list);
		for(int i=0;i<3;i++){
			View view = this.inflate(R.layout.chat_item);
			TextView text = (TextView) view.findViewById(R.id.msg);
//			text.setBackgroundResource(R.drawable.sms_in_normal_bg);
//			text.setTextColor(0xff0000ff);
//			text.setText("chat-"+i);
			ll.addView(view);
			text.setOnClickListener(clickListener);
		}
		
		Button btn_send = (Button) this.findViewById(R.id.button_send);
		btn_send.setOnClickListener(sendMsg);
    }
    
    private View inflate(int resource){
    	LayoutInflater layoutInflater = LayoutInflater.from(this);
    	View view = layoutInflater.inflate(resource, null);
		return view;
    }
    
    private void onSendClick(){
    	EditText input = (EditText)this.findViewById(R.id.editMsg);
    	final ScrollView chat_scroll = (ScrollView)this.findViewById(R.id.chat_scroll);
		String msg = input.getText().toString();
		View view = this.getMsgView(msg, 1, new Date());
		
		LinearLayout ll = (LinearLayout) this.findViewById(R.id.chat_list);
		ll.addView(view);
		input.setText(null);
		
		chat_scroll.post(new Runnable() {
	           public void run() {
	        	   chat_scroll.fullScroll(View.FOCUS_DOWN);     
	           }     
	       });   
    }
    
    private View getMsgView(String msg, int state, Date date){
    	LinearLayout chat_item_view = (LinearLayout)this.inflate(R.layout.chat_item2);
		TextView text = (TextView) chat_item_view.findViewById(R.id.msg);
		text.setText(msg);
		text.setOnClickListener(clickListener);
    	return chat_item_view;
    }
    
    OnClickListener sendMsg = new OnClickListener(){
    	public void onClick(View v) {
    		onSendClick();
    	}
    };
    
	OnClickListener clickListener=new OnClickListener(){
		public void onClick(View v) {
			Toast.makeText(ChatActivity.this, "chat...",Toast.LENGTH_LONG).show();
		}  
    };
}
